package br.com.gaacorp.sai.webapp.action;

import java.io.Serializable;

import br.com.gaacorp.sai.model.Campus;
import br.com.gaacorp.sai.webapp.action.BasePage;
import br.com.gaacorp.sai.service.GenericManager;

public class CampusForm extends BasePage implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 7643108015399489804L;
	private GenericManager<Campus, Long> campusManager;
	private Campus campus = new Campus();
	private Long campusId;

	public String delete() {
		campusManager.remove(campus.getCampusId());
		addMessage("campus.deleted");

		return "campusList";
	}

	public String edit() {
		// Comparison to zero (vs. null) is required with MyFaces 1.2.2, not
		// with previous versions
		if (campusId != null && campusId != 0) {
			campus = campusManager.get(campusId);
		} else {
			campus = new Campus();
		}

		return "campusForm";
	}

	public Campus getCampus() {
		return campus;
	}

	public String getCountry() {
		return getCampus().getAddress().getCountry();
	}

	public String save() {
		boolean isNew = (campus.getCampusId() == null || campus.getCampusId() == 0);
		campusManager.save(campus);

		String key = (isNew) ? "campus.added" : "campus.updated";
		addMessage(key);

		if (isNew) {
			return "campusList";
		} else {
			return "campusForm";
		}
	}

	public void setCampus(Campus campus) {
		this.campus = campus;
	}

	public void setCampusId(Long campusId) {
		this.campusId = campusId;
	}

	public void setCampusManager(GenericManager<Campus, Long> campusManager) {
		this.campusManager = campusManager;
	}

	public void setCountry(String country) {
		getCampus().getAddress().setCountry(country);
	}
}